package com.zonski.jbots.game.entity;

import com.zonski.jbots.engine.Modes;
import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.EntityFactory;
import com.zonski.jbots.engine.entity.AbstractMind;
import com.zonski.jbots.engine.entity.Mind;

import com.zonski.jbots.game.JBotsConstants;
import com.zonski.jbots.game.EntityHelper;

public class BombMind extends AbstractMind
{
    public static final int DEFAULT_COUNT_TO = 100;

    private int countTo = DEFAULT_COUNT_TO;

    /**
     * A new mode kind
     */
    public static final int PRE_EXPLOSION = 3;

    public BombMind()
    {
    }

    public final int getCountTo()
    {
        return this.countTo;
    }

    public void update(Engine engine)
        throws Exception
    {
        int count = getEntity().getEffectiveModeUpdates();
        if(count > (countTo*3)/4 && getEntity().getMode() == Modes.NONE)
        {
            getEntity().setMode(PRE_EXPLOSION);
            getEntity().setEffectiveMode(PRE_EXPLOSION);
        }
        if(count > countTo || getEntity().getMode() == Modes.DEAD)
        {
            // BOOM!
            explode(engine);
        }
    }

    // ignore collisions
    
    private void explode(Engine engine)
        throws Exception
    {
        //EntityHelper.addEntity(JBotsConstants.EXPLOSION, getEntity(), engine, getEntity().layerId);
        EntityHelper.addEntity(JBotsConstants.EXPLOSION, getEntity(), engine, 99);
        // we're dead
        this.getEntity().setRemovable(true);
    }

    public Mind copy()
    {
        return new BombMind();
    }
}
